package d3_method_reference;


import java.util.Arrays;
import java.util.Comparator;

public class Test1 {
    public static void main(String[] args) {
        //目标: 理解静态方法引用
        Student[] students = new Student[4];
        //进行数据封装
        students[0] = new Student("周芷若", 21, '女', 169.3);
        students[1] = new Student("殷素素", 38, '女', 172.2);
        students[2] = new Student("小昭", 19, '女', 168.5);
        students[3] = new Student("张无忌", 23, '男', 183.5);


        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return Double.compare(o1.getHeight(), o2.getHeight());
            }
        });

        //两个参数不能省---->担心一个参数属于sort方法,因此只能在一个参数的时候省略
        Arrays.sort(students, (Student o1, Student o2) -> {
            return Double.compare(o1.getHeight(), o2.getHeight());
        });
        //多个参数是,不能省略小括号(担心o1属于sort方法)
        Arrays.sort(students, (o1, o2) -> {
            return Double.compare(o1.getHeight(), o2.getHeight());
        });
        //为什么要搞出Lambda表达式,因为代码过于臃肿-->不是比较好的语言,所以简化一下
        Arrays.sort(students, (o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()));//sort底层会调用重写之后的方法


        //静态方法引用进行简化
        //(注意:规定就是规定,没有道理) 格式: 类名::静态方法
        //进一步进行简化--->如果某个Lambda表达式里只能调用一个静态方法,并且前后参数的形式一致,就可以使用静态方法引用
        Arrays.sort(students, (o1, o2) ->Student.compareByHeight(o1,o2));

        Arrays.sort(students, Student::compareByHeight);//终级简化代码
        //规则(符合规定):去掉前后前后参数,与箭头,将点改成::即可

        Arrays.sort(students, Test1::compareByHeight);//终级简化代码

        System.out.println(Arrays.toString(students));
    }
    public static int compareByHeight(Student o1, Student o2){
        return Double.compare(o1.getHeight(),o2.getHeight());
    }
}
